package com.cuit.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.FileService;
import com.cuit.service.SMSService;
import com.cuit.service.UserService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.cuit.utils.HashUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author chishuiLi     Email:1192537447@qq.com
 * @Description
 */
@CrossOrigin //跨域
@Api(tags = "注册页")
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Resource
    private FileService fileService;

    @Resource
    private UserService userService;

    @Resource
    private SMSService smsService;

    @RequestMapping("/uploadAvatar")
    @ApiOperation("注册上传信息")
    public R uploadAvatar(@RequestParam("user") String  userStr, @RequestParam("file") MultipartFile file){

        System.out.println(userStr);
        System.out.println(file);
        User user = JSON.parseObject(userStr, User.class);

        //取验证码
        JSONObject object = JSONObject.parseObject(userStr);
        String code = object.getString("code");
        System.out.println("验证码是：" + code);

        //取两次密码对比
        String password = object.getString("password");
        String repassword = object.getString("repassword");

        if(password == null || repassword == null || !password.equals(repassword)){
            return R.error().data("errMsg","密码输入有误");
        }
        if(code == null || user.getPhone() == null){
            return R.error().data("errMsg","手机号或验证码有误");
        }
        //发送验证码
//        String smsCode = smsService.sms(user.getPhone());
//        System.out.println("短信生成的验证码是"+ smsCode);

        if(!smsService.phoneCodeIsSuccess(user.getPhone(),code)){
            return R.error().data("errMsg","手机号或验证码有误");
        }

        //手机号，密码，验证码校验成功，开始往数据库写入信息
        //对密码进行加密处理
        user.setPassword(HashUtils.encryPassword(user.getPassword()));


        try {
            String fileUrl = fileService.getImgPathSQL(file); //得到存入数据库的路径
            System.out.println(fileUrl);
            user.setAvatar(fileUrl);

            //往数据库插入
            boolean success = userService.save(user);

            return (success?R.ok().data("imgUrl",fileUrl):R.error());
        } catch (IOException e) {
            return R.error().data("errMsg","服务器出错了");
        }

    }

    @PostMapping("/sendSMS/{phone}")
    @ApiOperation("短信测试接口")
    public R sendSMS(@PathVariable("phone") String phone){
        System.out.println(phone);
        String smsMsg = smsService.sms(phone);
        System.out.println(smsMsg);
        if(smsMsg.length() != 6){
            return R.error().data("errMsg",smsMsg);
        }
        return R.ok().data("验证码",smsMsg);

    }

}
